package impl;

import bean.Role;
import dao.RoleDao;
import util.DBHelper;

import java.sql.*;

/**
 * @classname:RoleDaoImpl
 * @description:
 * @author: Mr.Z
 * @date: 2020/12/29 21:30
 * @version: 0.1
 * @since:1.8
 */
public class RoleDaoImpl implements RoleDao {
    private Connection conn;
    private String sql;
    private PreparedStatement ps;
    private int result;
    private ResultSet rs;

    public RoleDaoImpl(Connection conn) {
        this.conn = conn;
    }

    public RoleDaoImpl() {
    }

    //添加角色，获得角色Id Statement.RETURN_GENERATED_KEYS, rs = ps.getGeneratedKeys();
    @Override
    public int addRole(Role role) {
//        conn = DBHelper.getCon();
        sql = "insert into tb_role(rolename,roledesc) values(?,?)";
        long pid = 0;
        try {
            ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            ps.setString(1, role.getRoleName());
            ps.setString(2, role.getRoleDesc());
            //获得自增的id select last_insert_id;
            ps.executeUpdate();
            rs = ps.getGeneratedKeys();
            if (rs.next()) {
                pid = rs.getLong(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return (int) pid;
    }


    //添加中间表信息StringBuilder拼接
    @Override
    public int addRoleAndPer(int id, int[] pids) {
//        conn = DBHelper.getCon();
        StringBuilder builder = new StringBuilder();
        for (int pid : pids) {
            builder.append("(");
            builder.append(id);
            builder.append(",");
            builder.append(pid);
            builder.append(")");
            builder.append(",");
        }
        builder.deleteCharAt(builder.lastIndexOf(","));
        sql = "insert into tb_role_per(roleid,pid) values " + builder;
        try {
            ps = conn.prepareStatement(sql);
            result = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    //更新角色信息通过id
    @Override
    public int updateRoleId(Role role) {
        sql = "update tb_role set rolename=? , roledesc=? where id=?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, role.getRoleName());
            ps.setString(2, role.getRoleDesc());
            ps.setInt(3, role.getId());
            result = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return result;
    }

    //查询角色信息通过id
    @Override
    public Role seleteRoleById(int id) {
        conn = DBHelper.getCon();
        sql = "select * from tb_role where id=?";
        Role role = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            rs = ps.executeQuery();
            if (rs.next()) {
                role = new Role(rs);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return role;
    }

    //删除角色信息
    @Override
    public void deleteRoleAndPer(Integer id) {

        sql = "delete  from tb_role_per where roleid = ?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            result = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
